<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div [formGroup]="connectorForm" class="add-connector">
  <mat-toolbar color="primary">
    <h2>{{ "gateway.add-connector" | translate}}</h2>
    <span fxFlex></span>
    <div [tb-help]="helpLinkId()"></div>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content>
    <div class="tb-form-panel no-border no-padding" fxLayout="column">
      <div class="tb-form-row column-xs" fxLayoutAlign="space-between center">
        <div class="fixed-title-width" translate>gateway.type</div>
        <div class="tb-flex no-gap">
          <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="type">
              <mat-option *ngFor="let type of gatewayConnectorDefaultTypesTranslatesMap | keyvalue" [value]="type.key">
                {{ type.value }}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </div>
      </div>
      <div class="tb-form-row column-xs" fxLayoutAlign="space-between center">
        <div class="fixed-title-width tb-required" translate>gateway.name</div>
        <div class="tb-flex no-gap">
          <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
            <input matInput autocomplete="off" name="value" formControlName="name" placeholder="{{ 'gateway.set' | translate }}"/>
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                      [matTooltip]="(connectorForm.get('name').hasError('duplicateName') ?
                                    'gateway.connector-duplicate-name' :'gateway.name-required') | translate"
                      *ngIf="(connectorForm.get('name').hasError('required') && connectorForm.get('name').touched)
                            || connectorForm.get('name').hasError('duplicateName')"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
        </div>
      </div>
      <div *ngIf="connectorForm.get('type').value === connectorType.CUSTOM" class="tb-form-row column-xs" fxLayoutAlign="space-between center">
        <div class="fixed-title-width" translate>gateway.connectors-table-class</div>
        <div class="tb-flex no-gap">
          <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
            <input matInput name="value" formControlName="class" placeholder="{{ 'gateway.set' | translate }}"/>
          </mat-form-field>
        </div>
      </div>
      <div *ngIf="connectorForm.get('type').value === connectorType.GRPC" class="tb-form-row column-xs" fxLayoutAlign="space-between center">
        <div class="fixed-title-width" translate>gateway.connectors-table-key</div>
        <div class="tb-flex no-gap">
          <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
            <input matInput name="value" formControlName="key" placeholder="{{ 'gateway.set' | translate }}"/>
          </mat-form-field>
        </div>
      </div>
      <div class="tb-form-row column-xs" fxLayoutAlign="space-between center">
        <div class="fixed-title-width" translate>gateway.remote-logging-level</div>
        <div class="tb-flex no-gap">
          <mat-form-field class="tb-flex no-gap" appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="logLevel">
              <mat-option *ngFor="let logLevel of gatewayLogLevel" [value]="logLevel">{{ logLevel }}</mat-option>
            </mat-select>
          </mat-form-field>
        </div>
      </div>
      <div *ngIf="connectorForm.get('type').value !== connectorType.GRPC && connectorForm.get('type').value !== connectorType.CUSTOM"
           class="tb-form-row column-xs" fxLayoutAlign="space-between center">
        <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="useDefaults">
          <mat-label tb-hint-tooltip-icon="{{ 'gateway.fill-connector-defaults-hint' | translate }}">
            {{ 'gateway.fill-connector-defaults' | translate }}
          </mat-label>
        </mat-slide-toggle>
      </div>
      <div *ngIf="connectorForm.get('type').value === connectorType.MQTT" class="tb-form-row column-xs" fxLayoutAlign="space-between center">
        <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="sendDataOnlyOnChange">
          <mat-label tb-hint-tooltip-icon="{{ 'gateway.send-change-data-hint' | translate }}">
            {{ 'gateway.send-change-data' | translate }}
          </mat-label>
        </mat-slide-toggle>
      </div>
    </div>
  </div>
  <div mat-dialog-actions fxLayoutAlign="end center">
    <button mat-button color="primary"
            type="button"
            cdkFocusInitial
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            (click)="add()"
            [disabled]="connectorForm.invalid || !connectorForm.dirty">
      {{ 'action.add' | translate }}
    </button>
  </div>
</div>
